package com.gitee.ywj1352.leecode;

import org.junit.Test;

public class StringDemo {

    /**
     * 翻转 string
     *
     * @param s
     * @return
     */
    public String reverseWords(String s) {
        String[] s1 = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = s1.length - 1; i >= 0; i--) {
            sb.append(s1[i]);
            if (i != 0) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }


    @Test
    public void reverseWords() {
        String s = "wo ai ni wo de zu guo";
        String s1 = reverseWords(s);
        System.out.println(s1);
    }


    public int mySqrt(int x) {
        if (x == 1) return 1;
        long mid = x / 2;
        long head = 0;
        long tail = x;
        while (true) {
            if (x < mid * mid) {
                tail = mid;
                mid = (mid - head) / 2 + head;
                if (head == mid) {
                    return (int)mid;
                }
            } else if (x > mid * mid) {
                head = mid;
                mid = (tail - mid) / 2 + mid;
                if (head == mid) {
                    return (int)mid;
                }
            } else {
                return (int)mid;
            }
        }
    }

    @Test
    public void mySqrt() {

        System.out.println(mySqrt(2147395599));

    }
}
